package alone.sub.table.test.demo.mapper;

import alone.sub.table.test.demo.pojo.UserId;
import alone.sub.table.test.demo.pojo.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserInfoMapper {

    /**
     * 基础表插入数据获取ID 以用来判断后续插入哪张表
     * @param userId userId
     * @return num
     */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into sub_user_test_base values(null)")
    Long insertUserId(UserId userId);

    /**
     * zero插入数据
     * @param userInfo userInfo
     * @return num
     */
    @Insert("insert into sub_user_test_zero(`id`,`name`,`password`) values (#{userId},#{name},#{password})")
    Integer insertUserZero(UserInfo userInfo);

    /**
     * first插入数据
     * @param userInfo userInfo
     * @return num
     */
    @Insert("insert into sub_user_test_first(`id`,`name`,`password`) values (#{userId},#{name},#{password})")
    Integer insertUserFirst(UserInfo userInfo);

    /**
     * second插入数据
     * @param userInfo userInfo
     * @return num
     */
    @Insert("insert into sub_user_test_second(`id`,`name`,`password`) values (#{userId},#{name},#{password})")
    Integer insertUserSecond(UserInfo userInfo);

    /**
     * zero表中根据userId查询用户
     * @param userId userId
     * @return userInfo
     */
    @Select("select `id` as userId, `name`, `password` from sub_user_test_zero where `id` = #{userId}")
    UserInfo findZeroUserById(Integer userId);

    /**
     * first表中根据userId查询用户
     * @param userId userId
     * @return userInfo
     */
    @Select("select `id` as userId, `name`, `password` from sub_user_test_first where `id` = #{userId}")
    UserInfo findFirstUserById(Integer userId);
    /**
     * second表中根据userId查询用户
     * @param userId userId
     * @return userInfo
     */
    @Select("select `id` as userId, `name`, `password` from sub_user_test_second where `id` = #{userId}")
    UserInfo findSecondUserById(Integer userId);



}
